package com.crrcdt.vue_project.controller;

import com.crrcdt.vue_project.util.VerifyCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author lj on 2020/10/5.
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/api/private/v1")
public class VerifyCodeController {
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/vercode")
    public void code(HttpServletRequest req, HttpServletResponse resp, @RequestParam("num") Integer num) throws IOException {
        System.out.println("验证码" + req.getSession().getId());
        VerifyCode vc = new VerifyCode();
        BufferedImage image = vc.getImage();
        String text = vc.getText();
        final ValueOperations ops = redisTemplate.opsForValue();
        ops.set("index_code", text);
//        HttpSession session = req.getSession();
//        session.setAttribute("index_code", text);
        VerifyCode.output(image, resp.getOutputStream());
    }
}
